package com.example.note;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

public class Insert_note extends Activity {
	DatabaseHanler db;
	Context cm;
	// định nghĩa các hàm insert và refresh
	ActionBar actionBar;
	EditText title;
	EditText txtcontent;
	public String idNote = null;
	EditText search;
	ImageButton btnBack, btnShare, btnDel, btnNew;
	private static final int IMAGE_PICKER_SELECT = 999;
//	ImageView mSelectedImage ;

	public void getControl() {
		btnBack = (ImageButton) findViewById(R.id.btnBack);
		btnShare = (ImageButton) findViewById(R.id.btnShare);
		btnDel = (ImageButton) findViewById(R.id.btnDel);
		btnNew = (ImageButton) findViewById(R.id.btnNew);
//		mSelectedImage = (ImageView) findViewById(R.id.imageSelected);
	}

	public void setEvent() {
		btnBack.setOnClickListener(new ProcessMyEvent());
		btnShare.setOnClickListener(new ProcessMyEvent());
		btnDel.setOnClickListener(new ProcessMyEvent());
		btnNew.setOnClickListener(new ProcessMyEvent());

	}

	public class ProcessMyEvent implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.btnBack:
				finish();
				break;
			case R.id.btnShare:
				shareIntent();
				break;
			case R.id.btnDel:
				delete();
				break;
			case R.id.btnNew:
				idNote = null;
				Intent i = new Intent(Insert_note.this, Insert_note.class);
				startActivity(i);

				break;
			}
		}
	}

	public void shareIntent() {
		Notes n = db.getNote(Integer.parseInt(idNote));

		Intent sendIntent = new Intent();
		sendIntent.setAction(Intent.ACTION_SEND);
		sendIntent.putExtra(Intent.EXTRA_TEXT,
				n.getTitle() + " <> " + n.getContent());
		sendIntent.setType("text/plain");
		startActivity(Intent.createChooser(sendIntent,
				getResources().getText(R.string.action_share)));
	}

	public void getDatafromOtherActivity() {
		Intent intent = getIntent();
		Bundle extras = intent.getExtras();
		if (extras != null) {
			idNote = intent.getStringExtra("idNote");
			String title = intent.getStringExtra("title");
			String content = intent.getStringExtra("content");
			// String time = intent.getStringExtra("time");
			search.setText(title);
			txtcontent = (EditText) findViewById(R.id.txtcontent);
			txtcontent.setText(content);
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_insert_note);
		getControl();
		setEvent();
		db = new DatabaseHanler(this);
		cm = getApplicationContext();
		actionBar = getActionBar();
		// add the custom view to the action bar
		actionBar.setCustomView(R.layout.actionbar_view);
		search = (EditText) actionBar.getCustomView().findViewById(
				R.id.searchfield);
		search.setOnEditorActionListener(new OnEditorActionListener() {

			@Override
			public boolean onEditorAction(TextView v, int actionId,
					KeyEvent event) {
				Toast.makeText(Insert_note.this, "Search triggered",
						Toast.LENGTH_LONG).show();
				return false;
			}
		});

		actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM
				| ActionBar.DISPLAY_SHOW_CUSTOM);

		actionBar.setDisplayHomeAsUpEnabled(true);
		getDatafromOtherActivity();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.insert_note, menu);

		return true;
	}

	public void them() {
		actionBar = getActionBar();
		title = (EditText) actionBar.getCustomView().findViewById(
				R.id.searchfield);
		txtcontent = (EditText) findViewById(R.id.txtcontent);
		
		Notes n;
		n = new Notes();
		
		n.setTitle(title.getText().toString());
		n.setContent(txtcontent.getText().toString());
		n.setImg("abc");
		if (idNote == null) {
			db.addNote(n);
			Toast.makeText(this.cm, "Thêm thành công", Toast.LENGTH_SHORT)
					.show();

			this.finish();
		} else {
			n.setID(Integer.parseInt(idNote));
			db.updateNote(n);
			Toast.makeText(this.cm, "Cập nhật thành công", Toast.LENGTH_SHORT)
					.show();
			this.finish();
		}
	}

	public void delete() {
		db.deleteNote(idNote);
		Toast.makeText(this.cm, "Xoá thành công", Toast.LENGTH_SHORT).show();
		this.finish();
	}

	public void reset() {
		actionBar = getActionBar();
		title = (EditText) actionBar.getCustomView().findViewById(
				R.id.searchfield);
		txtcontent = (EditText) findViewById(R.id.txtcontent);
		title.setText("");
		txtcontent.setText("");
	}

	public void openDiaglog() {
		Intent i = new Intent(Intent.ACTION_PICK,
				android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
		startActivityForResult(i, IMAGE_PICKER_SELECT);
	}
	/**
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == IMAGE_PICKER_SELECT
				&& resultCode == Activity.RESULT_OK) {
			Insert_note activity = Insert_note.this;

			Bitmap bitmap = getBitmapFromCameraData(data, activity);
			mSelectedImage.setImageBitmap(bitmap);
		}
	}

	public static Bitmap getBitmapFromCameraData(Intent data, Context context) {
		Uri selectedImage = data.getData();
		String[] filePathColumn = { MediaStore.Images.Media.DATA };
		Cursor cursor = context.getContentResolver().query(selectedImage,
				filePathColumn, null, null, null);
		cursor.moveToFirst();
		int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
		String picturePath = cursor.getString(columnIndex);
		cursor.close();
		return BitmapFactory.decodeFile(picturePath);
	}
*/
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		switch (item.getItemId()) {
		case R.id.btnsave:
			them();
			break;
		case R.id.btnimg:
			openDiaglog();
			break;
		case R.id.btnrefresh:
			reset();
			break;

		}

		return super.onOptionsItemSelected(item);
	}
}
